﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TurboTemplate
{
    public partial class ParameterEntryDialog : Form
    {
        TransformInstructions m_Input;
        bool m_IsNew;

        public ParameterEntryDialog()
        {
            InitializeComponent();
        }

        public ParameterEntryDialog(TransformInstructions input, bool isNew = false) : this()
        {
            m_Input = input;
            m_IsNew = isNew;
        }

        private void ParameterEntryDialog_Load(object sender, EventArgs e)
        {

            var copy = new List<SchemaParameter>();
            foreach (var item in m_Input.ProcedureFromSchema.Parameters)
            {
                copy.Add((SchemaParameter)item.Clone());
            }

            grdParameters.DataSource = copy;

            grdParameters.Columns["ParameterName"].ReadOnly = true;
            grdParameters.Columns["ParameterName"].HeaderText = "Parameter";
            grdParameters.Columns["DataType"].ReadOnly = false;
            grdParameters.Columns["DataType"].HeaderText = "Data Type";
            grdParameters.Columns["UseNull"].DisplayIndex = 2;
            grdParameters.Columns["UseNull"].HeaderText = "Pass Null";
            grdParameters.Columns["Value"].DisplayIndex = 3;
            grdParameters.Columns["MaxLength"].Visible = false;

            chkExecute.Checked = m_Input.ProcedureFromSchema.ExecuteForData;

            if (m_IsNew)
            {
                txtEntityName.Text = (m_Input != null && m_Input.ProcedureFromSchema != null ? m_Input.ProcedureFromSchema.SPECIFIC_NAME : m_Input.TableFromSchema.TABLE_NAME);
            }
            else
            {
                txtEntityName.Text = m_Input.UserEnteredEntityName;
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Copy back!
            m_Input.ProcedureFromSchema.ExecuteForData = chkExecute.Checked;
            m_Input.ProcedureFromSchema.Parameters = grdParameters.DataSource as List<SchemaParameter>;
            m_Input.UserEnteredEntityName = txtEntityName.Text;

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void lnkReset_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var source = grdParameters.DataSource as List<SchemaParameter>;
            source.ForEach(x => {
                x.Value = null;
                x.UseNull = false;
            });
            grdParameters.Refresh();
        }

        private void grdParameters_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            chkExecute.Checked = true;
        }

    }
}
